//
//  PhotoBrowserCell.swift
//  LZFPhotoBrowser
//
//  Created by 梁梓烽 on 16/7/27.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

import UIKit
import SDWebImage
class PhotoBrowserCell: UICollectionViewCell {

    //图片控件属性
     lazy var imageView = UIImageView()
    
    //模型属性
    var shop:Shop? {
        
        didSet
        {
            //错误校验,小图url,如果小图url都没有的话,证明这张图片不行,返回,后面的操作不执行.
            guard let urlString = shop?.q_pic_url else {
                return
            
            }
            
           //从内存缓存中获取小图
            var smallImage = SDWebImageManager.sharedManager().imageCache.imageFromMemoryCacheForKey(urlString)
            if smallImage == nil{
                smallImage = UIImage(imageLiteral: "empty_picture")
            }
            
            imageView.frame = calculateFrame(smallImage)
            
            
            //大图Url
            guard let bigURLString =  shop?.z_pic_url else{
                 return
            }
            
            let bigUrl = NSURL(string: bigURLString)
           
            
            //在下载大图的过程中显示小图,下载完之后,显示大图
            imageView.sd_setImageWithURL(bigUrl, placeholderImage: smallImage) { (image, error, type, url) -> Void in
              
                self.imageView.frame = calculateFrame(image)
            
                }
            
        }
    }
    

    //初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(imageView)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
